package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.ProductSettingAdapter;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.ProductSettingModel;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class RefundOptionActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ImageView img_title;
    private TextView text_title;
    private ListView list_option;
    private String mType;
    private String[] mServiceData={"仅退货", "退货退款"};
    private String[] mStatus={"未收到货","已收到货"};
    private ProductSettingAdapter mAdapter;
    private List<ProductSettingModel> mData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_option);
        Tools.changeSystemBar(this);
        initViews();
    }

    @Override
    public void findViewByIds() {
        img_title = (ImageView) findViewById(R.id.img_title);
        text_title = (TextView) findViewById(R.id.text_title);
        list_option = (ListView) findViewById(R.id.list_option);
    }

    @Override
    public void initListener() {
        img_title.setOnClickListener(this);
        list_option.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        mType = intent.getStringExtra("type");
        if ("1".equals(mType)) {
            text_title.setText("申请服务");
            list_option.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mServiceData));
        }else if("2".equals(mType)){
            text_title.setText("退货原因");
            mData=new ArrayList<>();
            mAdapter=new ProductSettingAdapter(this);
            list_option.setAdapter(mAdapter);
            getReason();
        }else if("3".equals(mType)){
            text_title.setText("货物状态");
            list_option.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStatus));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent=new Intent();
        if("1".equals(mType)){
            intent.putExtra("data", mServiceData[i]);
            setResult(Constant.GET_SERVICE_RESULT, intent);

        }else if("2".equals(mType)){
            intent.putExtra("id",mData.get(i).getId());
            intent.putExtra("data", mData.get(i).getLabel());
            Log.e("TAG","id:"+mData.get(i).getId());
            setResult(Constant.GET_REASON_RESULT, intent);
        }else if("3".equals(mType)){
            intent.putExtra("data",mStatus[i]);
            setResult(Constant.GET_STATUS_RESULT,intent);
        }
        finish();
    }
    private void getReason(){
        final LoadingDialog loadingDialog=new LoadingDialog(this);
        loadingDialog.setMsg("正在请求数据");
        loadingDialog.show();
        RequestParams params=new RequestParams();
        params.addBodyParameter("twCode","04");
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.COMMON_DATA, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                Log.e("TAG","get reason response:"+responseInfo.result);
                if(TextUtils.isEmpty(responseInfo.result)){
                    showMsg("暂无数据");
                }else{
                    try {
                        JSONObject root=new JSONObject(responseInfo.result);
                        String code=root.getString("code");
                        if("100".equals(code)){
                            JSONArray list=root.getJSONArray("list");
                            if(list.length()>0){
                                mData.clear();
                                for(int i=0;i<list.length();i++){
                                    JSONObject value=list.getJSONObject(i);
                                    ProductSettingModel model=new ProductSettingModel();
                                    model.setLabel(value.getString("twName"));
                                    model.setType("1");
                                    model.setId(value.getInt("twId"));
                                    model.setPicUrl(value.getString("twCode"));
                                    mData.add(model);
                                }
//                                TODO 选要修改
//                                mAdapter.setData(mData);
                                mAdapter.notifyDataSetChanged();
                            }else{
                                showMsg("暂无数据");
                            }
                        }else{
                            String msg=root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                showMsg("当前网络不可用,请检查您的网络设置");
                Log.e("TAG","get reason failure:"+s);
            }
        });
    }
}
